package com.tanqing.ddd.web.domain.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanqing.ddd.domain.aggregate.Parkting;
import com.tanqing.ddd.domain.aggregate.ParktingImpl;
import com.tanqing.ddd.domain.entity.Plate;
import com.tanqing.ddd.domain.repository.ParktingRepository;
import com.tanqing.ddd.domain.utils.DateUtils;
import com.tanqing.ddd.web.entity.CfCarParkUseLog;
import com.tanqing.ddd.web.mapper.CfCarParkUseLogMapper;
import com.tanqing.ddd.web.service.ICfCarParkUseLogService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ParkingRepositoryImpl extends ServiceImpl<CfCarParkUseLogMapper, CfCarParkUseLog> implements ParktingRepository, ICfCarParkUseLogService {

    @Override
    public Parkting fundByPlate(Plate plate) {
        QueryWrapper<CfCarParkUseLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CfCarParkUseLog::getNumberPlate, plate.getPlateNumber()).last("  limit 1");
        CfCarParkUseLog one = this.getOne(queryWrapper);
        if (one == null) {
            ParktingImpl parkting = new ParktingImpl();
            parkting.setPlate(plate);
//            parkting.setCheckInTime(DateUtils.formDateTime("2023-01-01 00:00:00"));
//            parkting.setLastPayTime(DateUtils.formDateTime("2023-01-01 00:00:00"));
            ;
            return parkting;
        } else {
            ParktingImpl parkting = new ParktingImpl();
            parkting.setPlate(plate);
            parkting.setCheckInTime(DateUtils.getDateByTimestamp(one.getInTime()));
            parkting.setLastPayTime(DateUtils.getDateByTimestamp(one.getPayTime()));
            parkting.setCheckOutTime(DateUtils.getDateByTimestamp(one.getOutTime()));
            ;
            return parkting;
        }
    }

    @Override
    public void save(Parkting parkting) {
        CfCarParkUseLog carParkUseLog = new CfCarParkUseLog();
        if (parkting.getParams().getCheckInTime() != null) {
            carParkUseLog.setInTime(DateUtils.getTimestamp(parkting.getParams().getCheckInTime()));
        }
        if (parkting.getParams().getCheckOutTime() != null) {
            carParkUseLog.setOutTime(DateUtils.getTimestamp(parkting.getParams().getCheckOutTime()));
        }
        if (parkting.getParams().getLastPayTime() != null) {
            carParkUseLog.setPayTime(DateUtils.getTimestamp(parkting.getParams().getLastPayTime()));
        }
        if (parkting.getParams().getTotalPayAmount() != null) {
            carParkUseLog.setCarBrand(parkting.getParams().getTotalPayAmount().toString());
        }
        carParkUseLog.setCreateTime(DateUtils.getTimestamp(new Date()));
        carParkUseLog.setNumberPlate(parkting.getParams().getPlate().getPlateNumber());
        carParkUseLog.setCarParkId("1");
        carParkUseLog.setCreatePosition("abc");
        this.save(carParkUseLog);
    }
}
